<?php
/**
 * Created by PhpStorm.
 * User: zd
 * Date: 2018/11/14
 * Time: 16:06
 */

namespace app\api\validate;


use app\lib\exception\CategoryException;
use app\lib\exception\ParameterException;

class PropertyParameter extends BaseValidate
{
    protected $rule=[
        'delete'=>'checkDeletes',
        'add'=>'checkAdds',
    ];

    protected $singleRule=[
        'id'=>'require|isPositiveInt',
    ];


    protected  $message=[
        'delete'=>'删除规格的数组不能为空',
        'add'=>'添加规格的数组不能为空'
    ];

    /**
     * 验证删除的商品规格id
     * @param $values
     * @return bool
     */
    protected function checkDeletes($values){
        if (!empty($values)){
            foreach ($values as $value){
                $this->checkDelete($value);
            }
        }
        return true;
    }
    /**
     * 验证单个商品规格
     * @param $product
     * @return bool
     * @throws CategoryException
     */
     protected function checkDelete($product){
        $validate  = new BaseValidate($this->singleRule);
        $result = $validate->check($product);
        if (!$result){
            throw new CategoryException([
                'msg'=>'删除的规格id不正确'
            ]);
        }
        return true;
    }

    /**
     *验证字符类容
     * @param $valus
     * @return bool
     */
    protected function checkAdds($values){
        if (!empty($values)){
            foreach ($values as $value){
                $this->checkAdd($value);
            }
        }
        return true;
    }

    /**
     * 验证敏感字符
     * @param $name
     * @return bool
     * @throws ParameterException
     */
    protected function checkAdd($name){
        if (in_array($name,['共产党','新华扬'])){
            throw new ParameterException([
                'msg'=>'不能有敏感字符'.$name
            ]);
        }
        return true;
    }
}